package com.xunyin.modules.mainpart.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.area.domain.TAreaSpace;
import com.xunyin.modules.area.service.TAreaSpaceService;
import com.xunyin.modules.index.service.dto.IndexDto;
import com.xunyin.modules.mainpart.domain.TMainPart;
import com.xunyin.modules.mainpart.service.TMainPartService;
import com.xunyin.modules.mainpart.service.dto.TMainPartDto;
import com.xunyin.modules.mainpart.service.dto.TMainPartQueryCriteria;
import com.xunyin.modules.mainpart.service.mapper.TMainPartMapper;
import com.xunyin.modules.mainpart.vo.TMainPartOnlyNameVo;
import com.xunyin.modules.mainpart.vo.TMainPartVo;
import com.xunyin.modules.membersuser.service.TMembersUserService;
import com.xunyin.modules.shareorder.domain.TShareOrder;
import com.xunyin.modules.shareorder.service.TShareOrderService;
import com.xunyin.modules.shareorder.service.mapper.TShareOrderMapper;
import com.xunyin.modules.user.service.SysXbAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-09-13
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TMainPartServiceImpl extends BaseServiceImpl<TMainPartMapper, TMainPart> implements TMainPartService {

    @Autowired
    private IGenerator generator;

    @Autowired
    private TAreaSpaceService tAreaSpaceService;

    @Autowired
    private SysXbAppUserService sysXbAppUserService;

    @Autowired
    private TShareOrderService tShareOrderService;

    @Autowired
    private TMembersUserService tMembersUserService;

    @Autowired
    private TShareOrderMapper tShareOrderMapper;

    /**
     * 首页订单/用户等统计
     *
     * @return IndexDto
     */
    @Override
    public IndexDto getIndexDataStat() {

        IndexDto indexDto = new IndexDto();
        indexDto.setUserCount(sysXbAppUserService.count());
        indexDto.setOrderCount(tShareOrderService.count());
        indexDto.setPriceCount(tShareOrderMapper.sumRealOrderMoney(0));
        indexDto.setMemberCount(tMembersUserService.count());

        Date today = DateUtil.beginOfDay(new Date());
        Date yesterday = DateUtil.beginOfDay(DateUtil.yesterday());
        Date nowMonth = DateUtil.beginOfMonth(new Date());
        Date lastWeek = DateUtil.beginOfDay(DateUtil.lastWeek());

        //今日订单数
        LambdaQueryWrapper<TShareOrder> wrapperOne = new LambdaQueryWrapper<>();
        List<Integer> orderStatusIds = new ArrayList<>();
        orderStatusIds.add(1);
        orderStatusIds.add(2);
        orderStatusIds.add(4);
        wrapperOne.ge(TShareOrder::getPayTime, today)
                .in(TShareOrder::getOrderStatus, orderStatusIds);
        indexDto.setTodayCount(tShareOrderMapper.selectCount(wrapperOne));

        //昨日订单数
        LambdaQueryWrapper<TShareOrder> wrapperTwo = new LambdaQueryWrapper<>();
        wrapperTwo.lt(TShareOrder::getPayTime, today)
                .ge(TShareOrder::getPayTime, yesterday)
                .in(TShareOrder::getOrderStatus, orderStatusIds);
        indexDto.setProCount(tShareOrderMapper.selectCount(wrapperTwo));

        //上周订单数
        LambdaQueryWrapper<TShareOrder> wrapperLastWeek = new LambdaQueryWrapper<>();
        wrapperLastWeek
                .lt(TShareOrder::getPayTime, today)
                .ge(TShareOrder::getPayTime, lastWeek)
                .in(TShareOrder::getOrderStatus, orderStatusIds);
        indexDto.setLastWeekCount(tShareOrderMapper.selectCount(wrapperLastWeek));

        //本月订单数
        LambdaQueryWrapper<TShareOrder> wrapperThree = new LambdaQueryWrapper<>();
        wrapperThree
                .ge(TShareOrder::getPayTime, nowMonth)
                .in(TShareOrder::getOrderStatus, orderStatusIds);
        indexDto.setMonthCount(tShareOrderMapper.selectCount(wrapperThree));
        return indexDto;
    }

    /**
     * 查询数据分页
     *
     * @param criteria 条件
     * @param pageable 分页参数
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> queryAll(TMainPartQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TMainPart> page = new PageInfo<>(queryAll(criteria));
        List<TMainPartDto> tMainPartDTOS = new ArrayList<>();
        for (TMainPart tMainPart : page.getList()) {
            this.tMainPartList(tMainPartDTOS, tMainPart);
        }
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", tMainPartDTOS);
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<TMainPart> queryAll(TMainPartQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TMainPart.class, criteria));
    }

    /**
     * 转换主体的省、市、县名称
     *
     * @param tMainPartDTOS 主体列表
     * @param tMainPart     主体
     */
    private void tMainPartList(List<TMainPartDto> tMainPartDTOS, TMainPart tMainPart) {
        TMainPartDto tMainPartDto = generator.convert(tMainPart, TMainPartDto.class);

        // 设置省名称
        TAreaSpace tAreaSpaceProvince = tAreaSpaceService.getTAreaSpaceByAreaCode(tMainPart.getProvinceCode());
        tMainPartDto.setProvinceName(tAreaSpaceProvince.getAreaName());

        // 设置市名称
        TAreaSpace tAreaSpaceCity = tAreaSpaceService.getTAreaSpaceByAreaCode(tMainPart.getCityCode());
        tMainPartDto.setCityName(tAreaSpaceCity.getAreaName());

        // 设置县名称
        TAreaSpace tAreaSpaceCounty = tAreaSpaceService.getTAreaSpaceByAreaCode(tMainPart.getCountyCode());
        tMainPartDto.setCountyName(tAreaSpaceCounty.getAreaName());
        tMainPartDTOS.add(tMainPartDto);
    }

    /**
     * 获取主体列表
     *
     * @return List<TMainPartVo>
     */
    @Override
    public List<TMainPartVo> listMainPart() {
        LambdaQueryWrapper<TMainPart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TMainPart::getIsDel, 0);
        List<TMainPartVo> list = generator.convert(baseMapper.selectList(wrapper), TMainPartVo.class);
        return list;
    }

    /**
     * 根据小程序Id-获取主体
     *
     * @param appId
     * @return
     */
    @Override
    public TMainPartOnlyNameVo getMainPartByAppId(String appId) {
        LambdaQueryWrapper<TMainPart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TMainPart::getAppId, appId);
        TMainPartOnlyNameVo tMainPartOnlyNameVo = generator.convert(baseMapper.selectOne(wrapper), TMainPartOnlyNameVo.class);
        return tMainPartOnlyNameVo;
    }
}
